package com.boang.controller.owners;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boang.entity.PO.Members;
import com.boang.service.MembersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
@author:22567
@date:2025/3/5 17:09
*/
@Tag(name = "成员操作MembersController")
@RestController
@RequestMapping("/members")

public class MembersController {

    @Autowired
    private MembersService memberService;

    // 新增成员
    @PostMapping("/add")
    @Operation(summary = "新增成员")
    public String addMember(@RequestBody Members member) {
        try {
            memberService.save(member);
            return "新增成功";
        } catch (DuplicateKeyException e) {
            // 捕获并发插入冲突（兜底处理）
            return "身份证已存在";
        }


    }

    // 删除成员
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除成员")
    public String deleteMember(@PathVariable Integer id) {
        memberService.removeById(id);
        return "删除成功";
    }

    // 修改成员
    @PutMapping("/update")
    @Operation(summary = "修改成员")
    public String updateMember(@RequestBody Members member) {
        memberService.updateById(member);
        return "修改成功";
    }

    // 查询单个成员
    @GetMapping("/get/{id}")
    @Operation(summary = "查询单个成员")
    public Members getMember(@PathVariable Integer id) {
        return memberService.getById(id);
    }

    // 查询所有成员
    @GetMapping("/list")
    @Operation(summary = "查询所有成员")
    public List<Members> listMembers() {
        return memberService.list();
    }

    // 条件查询（示例：根据名称查询）
    @GetMapping("/search")
    @Operation(summary = "根据名称查询")
    public List<Members> searchMembers(@RequestParam String name) {
        QueryWrapper<Members> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name); // 模糊查询
        return memberService.list(queryWrapper);
    }
}
